﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hyt.Core.Interceptors;
using Hyt.Core;
using Hyt.Passport.QueryModels;
using Hyt.Core.Data;
using Hyt.Passport.Models;
using Hyt.Passport.Repositories;

namespace Hyt.Passport.Service
{
    public class PositionService 
    {
        public IPositionRepository Repository { get; set; }
        public DepartmentService DepartmentService { get; set; }

        public virtual Position Create(Position model)
        {
            Guard.CheckArg(model, "model");
            Guard.Check(model).PleaseInput(t => t.Name);
            model.Name = model.Name.Trim().ToLower();
            if (model.DepartmentId != null)
            {
                if (DepartmentService.Get( model.DepartmentId.Value)==null)
                {
                    Guard.Throw("部门不存在");
                }
                if (GetAll().Any(t => t.DepartmentId == model.DepartmentId && t.Name == model.Name))
                {
                    Guard.Throw("已存在相同名称的职位");
                }
            }
            else
            {
                if (GetAll().Any(t => t.DepartmentId ==null && t.Name == model.Name))
                {
                    Guard.Throw("已存在相同名称的职位");
                }
            }
            return Repository.AddPosition(model);
        }

        /// <summary>
        /// 修改职位名称
        /// </summary>
        /// <param name="model"></param>
        public void ModifyName(Position model)
        {
            Guard.CheckArg(model, "model");
            Guard.DontNull(model.Name,"职位名称");
            var position = Get(model.Id);
            Guard.MustExist(position,"职位");

            if (position.DepartmentId != null)
            {
                if (GetAll().Any(t => t.DepartmentId == position.DepartmentId && t.Name == model.Name&&t.Id!=position.Id))
                {
                    Guard.Throw("已存在相同名称的职位");
                }
            }
            else
            {
                if (GetAll().Any(t => t.DepartmentId == null && t.Name == model.Name && t.Id != position.Id))
                {
                    Guard.Throw("已存在相同名称的职位");
                }
            }
            position.Name = model.Name;
            Repository.UpdatePosition(position);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="model"></param>
        public void ModifyDepartment(Position model)
        {
            Guard.CheckArg(model, "model");
            var position = Get(model.Id);
            Guard.MustExist(position, "职位");
            if (position.DepartmentId == model.DepartmentId) return;

            if (model.DepartmentId != null)
            {
                if (GetAll().Any(t => t.DepartmentId == position.DepartmentId && t.Name == model.Name ))
                {
                    Guard.Throw("此部门已存在相同名称的职位");
                }
            }
            position.DepartmentId = model.DepartmentId;
            Repository.UpdatePosition(position);
        }

        public virtual IPagedQueryable<Position> GetPositions(PositionQueryModel qo)
        {
            IPagedQueryable<Position> result = new PagedQueryable<Position>();
            var all = GetAll(qo.QueryOption.Cast<Position>());
            if (!string.IsNullOrWhiteSpace(qo.Name))
            {
                all = all.Where(t => t.Name.Contains(qo.Name));
            }
            if (qo.DepartMentId != null && qo.DepartMentId != Guid.Empty)
            {
                var alldepts=DepartmentService.GetSelfAndChild(qo.DepartMentId);
                all = all.Where(t => alldepts.Any(d => d.Id == t.DepartmentId));
            }
            result.Total = all.Count();
            if (!string.IsNullOrWhiteSpace(qo.OrderBy))
            {
                all = all.SortBy(qo.OrderBy, qo.OrderType);
                all = all.PageBy(qo.PageIndex, qo.PageSize);
            }
            result.Data = all;
            result.PageIndex = qo.PageIndex;
            result.PageSize = qo.PageSize;
            return result;
            
        }

        public virtual IQueryable<Position> GetAll(QueryOption<Position> option = null)
        {
            return Repository.GetPositions(option);
        }

        public virtual Position Get(Guid id)
        {
            return Repository.GetPositions().FirstOrDefault(t => t.Id == id);
        }
        
        public virtual void Remove(Guid id)
        {
            Position p = Get(id);
            if (p == null)
            {
                Guard.Throw("职位不存在");
            }
            if (p.Administrators.Any())
            {
                Guard.Throw("不能删除存在用户的职位");
            }
            Repository.RemovePosition(p);
        }
    }
}
